package com.qmerak.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.alibaba.fastjson.JSON;
import com.qmerak.common.BSCalendar;
import com.qmerak.common.SysConstants;
import com.qmerak.entity.MemSalvageEntity;
import com.qmerak.entity.MemberEntity;
import com.qmerak.service.IMemSalvageService;
import com.qmerak.service.IMemberService;

/**
 * @author Shaohong•Cheng
 * @mail shaohong726@126.com
 * @date 2013-7-2
 * @project TUManager
 */
@Controller
@RequestMapping("/member/salvage")
@SessionAttributes({ "cur_user" })
public class MemSalvageAction {
	@Autowired
	private IMemSalvageService salvageService;
	@Autowired
	private IMemberService memberService;

	@ResponseBody
	@RequestMapping(value = "apply", method = RequestMethod.POST)
	public void apply(MemSalvageEntity salvageEntity, ModelMap modelMap) {
		MemberEntity loginMemberEntity = (MemberEntity) modelMap
				.get(SysConstants.SYS_LOGIN_USER);
		salvageEntity.setLocalOperator(loginMemberEntity.getId());
		salvageEntity.setApplyDate(BSCalendar.now().toString("yyyy-mm-dd"));
		salvageService.add(salvageEntity);
	}

	@ResponseBody
	@RequestMapping(value = "applieds", method = RequestMethod.GET)
	public List<Map<String, Object>> applieds(MemSalvageEntity salvageEntity,
			ModelMap modelMap) {
		MemberEntity loginMemberEntity = (MemberEntity) modelMap
				.get(SysConstants.SYS_LOGIN_USER);
		return buildResponseList(salvageService
				.findAppliedSalvages(loginMemberEntity));
	}

	@ResponseBody
	@RequestMapping(value = "unapproved", method = RequestMethod.GET)
	public List<Map<String, Object>> unapproved(MemSalvageEntity salvageEntity) {
		return buildResponseList(salvageService.findUnapproveSalvages());
	}

	@ResponseBody
	@RequestMapping(value = "approveAgree", method = RequestMethod.POST)
	public void approveAgree(MemSalvageEntity salvageEntity, ModelMap modelMap) {
		MemberEntity loginMemberEntity = (MemberEntity) modelMap
				.get(SysConstants.SYS_LOGIN_USER);
		MemSalvageEntity oldEntity = salvageService.find(salvageEntity.getId());
		oldEntity.setApproveDate(BSCalendar.now().toString("yyyy-mm-dd"));
		oldEntity.setApproveMoney(salvageEntity.getApproveMoney());
		oldEntity.setApprover(loginMemberEntity.getId());
		oldEntity.setApproveSuggest(salvageEntity.getApproveSuggest());
		oldEntity.setState(MemSalvageEntity.SalvageState.APPROVEDAGREE.name());
		salvageService.update(oldEntity);
	}

	@ResponseBody
	@RequestMapping(value = "approveDisagree", method = RequestMethod.POST)
	public void approveDisagree(MemSalvageEntity salvageEntity,
			ModelMap modelMap) {
		MemberEntity loginMemberEntity = (MemberEntity) modelMap
				.get(SysConstants.SYS_LOGIN_USER);
		MemSalvageEntity oldEntity = salvageService.find(salvageEntity.getId());
		oldEntity.setApproveDate(BSCalendar.now().toString("yyyy-mm-dd"));
		oldEntity.setApproveMoney(salvageEntity.getApproveMoney());
		oldEntity.setApprover(loginMemberEntity.getId());
		oldEntity.setApproveSuggest(salvageEntity.getApproveSuggest());
		oldEntity.setState(MemSalvageEntity.SalvageState.APPROVEDDISAGREE
				.name());
		salvageService.update(oldEntity);
	}

	@ResponseBody
	@RequestMapping(value = "unfeedbacks", method = RequestMethod.GET)
	public List<Map<String, Object>> unfeedbacks(
			MemSalvageEntity salvageEntity, ModelMap modelMap) {
		MemberEntity loginMemberEntity = (MemberEntity) modelMap
				.get(SysConstants.SYS_LOGIN_USER);
		return buildResponseList(salvageService
				.findUnfeedbackSalvages(loginMemberEntity));
	}

	@ResponseBody
	@RequestMapping(value = "feedback", method = RequestMethod.POST)
	public void feedback(MemSalvageEntity salvageEntity, ModelMap modelMap) {
		MemberEntity loginMemberEntity = (MemberEntity) modelMap
				.get(SysConstants.SYS_LOGIN_USER);
		MemSalvageEntity oldEntity = salvageService.find(salvageEntity.getId());
		oldEntity.setFeedback(salvageEntity.getFeedback());
		oldEntity.setFeedbackDate(BSCalendar.now().toString("yyyy-mm-dd"));
		oldEntity.setFeedbackPerson(loginMemberEntity.getId());
		oldEntity.setState(MemSalvageEntity.SalvageState.FEEDBACKED.name());
		salvageService.update(oldEntity);
	}

	@ResponseBody
	@RequestMapping(value = "feedbackeds", method = RequestMethod.GET)
	public List<Map<String, Object>> feedbackeds(MemberEntity memberEntity) {
		return buildResponseList(salvageService
				.findFeedbackedSalvages(memberEntity));
	}

	@ResponseBody
	@SuppressWarnings("unchecked")
	private List<Map<String, Object>> buildResponseList(
			List<MemSalvageEntity> salvageEntities) {
		List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
		for (MemSalvageEntity entity : salvageEntities) {
			Map<String, Object> itemMap = (Map<String, Object>) JSON
					.toJSON(entity);
			for (Entry<String, Object> entry : itemMap.entrySet()) {
				if (null == entry.getValue() || "null".equals(entry.getValue())) {
					entry.setValue("");
				}
			}
			resultList.add(itemMap);
			String memberID = entity.getMemberID();
			if (null != memberID && !"".equals(memberID)
					&& !"null".equals(memberID)) {
				MemberEntity memberEntity = memberService.find(memberID);
				if (null != memberEntity) {
					itemMap.put("memberName", memberEntity.getName());
				}
			}
			memberID = entity.getLocalOperator();
			if (null != memberID && !"".equals(memberID)
					&& !"null".equals(memberID)) {
				MemberEntity memberEntity = memberService.find(memberID);
				if (null != memberEntity) {
					itemMap.put("applyerName", memberEntity.getName());
				}
			}
			memberID = entity.getApprover();
			if (null != memberID && !"".equals(memberID)
					&& !"null".equals(memberID)) {
				MemberEntity memberEntity = memberService.find(memberID);
				if (null != memberEntity) {
					itemMap.put("approverName", memberEntity.getName());
				}
			}
			memberID = entity.getFeedbackPerson();
			if (null != memberID && !"".equals(memberID)
					&& !"null".equals(memberID)) {
				MemberEntity memberEntity = memberService.find(memberID);
				if (null != memberEntity) {
					itemMap.put("feedbackerName", memberEntity.getName());
				}
			}
			switch (MemSalvageEntity.SalvageState.valueOf(entity.getState())) {
			case APPLYING:
				itemMap.put("stateText", "未审批");
				break;
			case APPROVEDAGREE:
				itemMap.put("stateText", "审批通过");
				break;
			case APPROVEDDISAGREE:
				itemMap.put("stateText", "审批未通过");
				break;
			case FEEDBACKED:
				itemMap.put("stateText", "已经反馈");
				break;
			default:
				break;
			}
		}
		return resultList;
	}
}
